﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Core.Interceptor;
using System.Reflection;

namespace Mbs.AOP.Test
{
    [Serializable]
    public class ComplexClass
    {
        private int _doMethodExecutionCount;
        private String _name;
        private bool _started;

        public virtual void DoNothing()
        {
        }

        public virtual int DoSomething()
        {
            return 1;
        }

        public virtual int DoSomething(int value1)
        {
            return value1;
        }

        public virtual int DoSomething(int value1, String text)
        {
            return value1;
        }

        public virtual int Pong(ref int i)
        {
            return i;
        }

        public virtual int DoMethodExecutionCount
        {
            get { return _doMethodExecutionCount; }
            set { _doMethodExecutionCount = value; }
        }

        public virtual String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public virtual bool Started
        {
            get { return _started; }
            set { _started = value; }
        }
    }

    public class LogInterceptor : IInterceptor
    {

        public LogInterceptor()
        {
        }

        public static void Clear()
        {
           HasIntercepted = false;
        }

        public static bool HasIntercepted { get; private set; }


        public void Intercept(IInvocation invocation)
        {
            MethodBase method = invocation.Method;
            Console.WriteLine();
          

            if (method.IsSpecialName)
            {
                Console.WriteLine("Invoking  property " + invocation.Method.Name);
            }
            else
            {
                Type[] args = Type.GetTypeArray(invocation.Arguments);

                String argMessage = "(";
                foreach (Type arg in args)
                {
                    argMessage += " " + arg.Name;
                }
                argMessage += ")";
              
                Console.WriteLine("Invoking  method " + invocation.Method.Name + argMessage);
            }

            invocation.Proceed();
            HasIntercepted = true;
        }

     
    }
}
